﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Windows;
using System.Windows.Input;
using ScrumTable.Common.Logging;

#endregion

namespace ScrumTable.UI.View.Util
{

    /// <summary>
    /// Description of the class
    /// </summary>
    public class ClickUtil
    {

        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private const double Clickdistance = 30;
        
        
#if SimulatorDebug
        private const int DoubleClickSpeedMs = 700;
        private readonly DateTime[] _times = new DateTime[2];
        private readonly Point[] _points = new Point[2];
#else
        private const int DoubleClickSpeedMs = 700;
        private readonly DateTime[] _times = new DateTime[2];
        private readonly Point[] _points = new Point[2];
#endif

        private int _i = 0;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------
        #endregion

        public ClickUtil()
        {
            Reset();
        }

        public ClickUtil(int clickCount)
        {
            _times = new DateTime[clickCount];
            Reset();
        }

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------
        
        /// <summary>
        /// Checks if the click is in time.
        /// </summary>
        /// <returns></returns>
      /*
        public bool IsClickInTime()
        {
            _times[_i] = DateTime.Now;
            int i2 = (_i + 1)%_times.Length;
            bool ret = (_times[_i] - _times[i2]).TotalMilliseconds < DoubleClickSpeedMs;
            _i = i2;
            if(ret)
            {
                Reset();   
            }
            return ret;
        }
        */


        public bool IsClickInTimeAndPlace(InputEventArgs args)
        {
            if( args is MouseEventArgs)
            {
                return IsClickInTimeAndPlace((MouseEventArgs) args);
            }
            else if (args is TouchEventArgs)
            {
                return IsClickInTimeAndPlace((TouchEventArgs)args);
            }
            else
            {
                Logger<ClickUtil>.Error("IsClickInTimeAndPlace: " + args.GetType());
            }
            return false;
        }

        public bool IsClickInTimeAndPlace(MouseEventArgs args)
        {

            return IsClickInTimeAndPlace(args.GetPosition(Application.Current.MainWindow));
        }

        public bool IsClickInTimeAndPlace(TouchEventArgs args)
        {

            return IsClickInTimeAndPlace(args.GetTouchPoint(Application.Current.MainWindow).Position);
        }

        /// <summary>
        /// Checks if the click is in time.
        /// </summary>
        /// <returns></returns>
        public bool IsClickInTimeAndPlace(Point position)
        {
            _times[_i] = DateTime.Now;
            _points[_i] = position;
            int i2 = (_i + 1) % _times.Length;
            bool isInTime = (_times[_i] - _times[i2]).TotalMilliseconds < DoubleClickSpeedMs;
            bool isPlace = (_points[_i] - _points[i2]).Length < Clickdistance; 
            _i = i2;
            if (isInTime && isPlace) 
            {
                Reset();
                return true;
            }
            return false;
        }

        private void Reset()
        {
            for(int i=0;i<_times.Length;i++)
            {
                _times[i] = DateTime.MinValue;
                _points[i] = new Point(double.MinValue, double.MinValue);
            }
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion

    }
}
